package cn.fl.insurance.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.fl.preloan.dto.ListInsurancePolicyResponseDTO;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Optional;
import com.lionbridge.log.ext.LogUtils;
import cn.fl.commons.result.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysCtrlCfgApiService;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.condition.SysCtrlCfgQueryCondition;
import cmm.comm.facade.upload.api.IUploadApiService;
import cmm.comm.facade.upload.condition.SysUlDatCfgQueryCondition;
import cmm.comm.facade.upload.dto.SysUlDatCfgDTO;
import cmm.comm.facade.upload.dto.SysUlDatCfgDtlDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.insurance.condition.InsCompChnPolQueryCondition;
import cn.fl.insurance.condition.InsPlcBscInfoQueryCondition;
import cn.fl.insurance.condition.InsPrjInfoQueryCondition;
import cn.fl.insurance.constant.InsuranceConstant;
import cn.fl.insurance.dto.InsCompChnPolDTO;
import cn.fl.insurance.dto.InsPlcBscInfoDTO;
import cn.fl.insurance.dto.InsPrjInfoDTO;
import cn.fl.insurance.service.IInsCompChnPolApiService;
import cn.fl.insurance.service.IInsPlcBscInfoApiService;
import cn.fl.insurance.service.IInsPlcBscInfoService;
import cn.fl.insurance.service.IInsTypInfoApiService;
import cn.fl.insurance.transform.InsPlcBscInfoVOTransform;
import cn.fl.insurance.transform.InsPrjInfoVOTransform;
import cn.fl.insurance.vo.InsPlcBscInfoVO;
import cn.fl.insurance.vo.InsPrjInfoVO;
import cn.fl.insurance.vo.PlcFileInfoVO;
import cn.fl.project.condition.PrjPrdDtlForInsQueryCondition;
import cn.fl.project.constant.PrjPrdDtlConstant;
import cn.fl.project.dto.PrjPrdDtlForInsDTO;
import cn.fl.project.service.IPrjPrdPaySchMApiService;
import cn.fl.project.service.IProjectApiService;
import cn.fl.project.transform.PrjBscInfoVOTransform;
import cn.fl.project.transform.PrjPrdDtlForInsVOTransform;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.project.vo.PrjPrdDtlForInsVO;
import fl.customer.facade.customer.api.ICustomerQueryApiService;
import fl.customer.facade.customer.dto.CstBscInfoDTO;
import fl.customer.facade.customer.dto.CstContactInfoDTO;

/**
 * @Description: 保单基础信息表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class InsPlcBscInfoServiceImpl implements IInsPlcBscInfoService {

    /**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(InsPlcBscInfoServiceImpl.class);

    /**
     * 自定义 API 暴露服务
     */
    @Autowired
    private IInsPlcBscInfoApiService insPlcBscInfoApiService;
    @Autowired
    private IPrjPrdPaySchMApiService prjPrdPaySchMApiService;

    /**
     * 数据字典 服务层BEAN
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    @Autowired
    private IInsTypInfoApiService insTypInfoApiService;
    @Autowired
    private ISysCtrlCfgApiService sysCtrlCfgApiService;
    @Autowired
    private ICustomerQueryApiService customerQueryApiService;
    @Autowired
    private IInsCompChnPolApiService insCompChnPolApiService;
    @Autowired
    private ISysOrgApiService sysOrgApiService;
    @Autowired
    private IProjectApiService projectApiService;
    @Autowired
    private IUploadApiService uploadApiService;
    @Autowired
    private cn.fl.preloan.IInsPlcBscInfoService iInsPlcBscInfoService;

    @Override
    public InsPlcBscInfoVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) return null;
        InsPlcBscInfoDTO dto = this.insPlcBscInfoApiService.selectInsPlcBscInfoById(id, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        return InsPlcBscInfoVOTransform.toVO(dto);
    }

    @Override
    public Long add(InsPlcBscInfoVO entity) throws BaseException {
        if (null == entity) return null;
        return this.insPlcBscInfoApiService.addInsPlcBscInfo(InsPlcBscInfoVOTransform.toDTO(entity));
    }

    @Override
    public boolean removeByIds(List<Long> ids) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) return false;
        return this.insPlcBscInfoApiService.removeInsPlcBscInfoByIds(ids, CurrentThreadContext.getCurrentUserId());
    }

    @Override
    public boolean modify(InsPlcBscInfoVO entity) throws BaseException {
        if (null == entity) return false;
        return this.insPlcBscInfoApiService.modifyInsPlcBscInfo(InsPlcBscInfoVOTransform.toDTO(entity));
    }

    @Override
    public boolean modify(List<InsPlcBscInfoVO> list) throws BaseException {
        if (CollectionUtils.isEmpty(list)) return false;
        return this.insPlcBscInfoApiService.modifyInsPlcBscInfo(InsPlcBscInfoVOTransform.toDTOList(list));
    }

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page) page = new Pager();
        page=this.insPlcBscInfoApiService.searchInsPlcBscInfoListPage(page, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        if(CollectionUtils.isNotEmpty(page.getResultList())){
            page.setResultList(InsPlcBscInfoVOTransform.toVOList((List<InsPlcBscInfoDTO>)page.getResultList()));
        }
        return page;
    }

    /**
     * 分页查询 投保管理
     *
     * @param page
     * @return
     */
    @Override
    public Pager searchInsureListPage(Pager page) throws BaseException {
        if (null == page) {
            page = new Pager();
        }
        page=this.insPlcBscInfoApiService.searchInsureListPage(page);
        if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<PrjPrdDtlForInsDTO> dtlForInsDTOList=(List<PrjPrdDtlForInsDTO>)page.getResultList();
            page.setResultList(PrjPrdDtlForInsVOTransform.toVOList(dtlForInsDTOList));
        }
        return page;
    }

    /**
     * 分页查询 投保管理
     *
     * @param queryCondition@return
     */
    @Override
    public List<PrjPrdDtlForInsVO> searchInsureList(PrjPrdDtlForInsQueryCondition queryCondition) throws BaseException {
        List<PrjPrdDtlForInsDTO> dtlForInsDTOList=this.insPlcBscInfoApiService.searchInsureList(queryCondition);
        if(CollectionUtils.isNotEmpty(dtlForInsDTOList)){
            return PrjPrdDtlForInsVOTransform.toVOList(dtlForInsDTOList);
        }
        return null;
    }

    /**
     * 根据项目产品明细ID查询产品列表
     * @param queryCondition
     * @return
     * @throws BaseException
     */
    public List<PrjPrdDtlForInsDTO> searchInsurePrdList(PrjPrdDtlForInsQueryCondition queryCondition) throws BaseException{
        return this.prjPrdPaySchMApiService.searchInsurePrdList(queryCondition);
    }


    /**
     * 分页查询 续保管理
     *
     * @param page
     * @return
     */
    @Override
    public Pager searchRenewalListPage(Pager page) throws BaseException {
        if (null == page) page = new Pager();
        InsPrjInfoQueryCondition condition=null;
        if(null==page.getCondition()){
            condition=new InsPrjInfoQueryCondition();
            page.setCondition(condition);
        }else{
            condition=(InsPrjInfoQueryCondition)page.getCondition();
        }
        SysCtrlCfgQueryCondition sysCtrlCfgQueryCondition=new SysCtrlCfgQueryCondition();
        sysCtrlCfgQueryCondition.setOrgCd(InsuranceConstant.CODE_INS_ORG_CD);
        sysCtrlCfgQueryCondition.setSysCd(InsuranceConstant.CODE_INS_SYS_CD);
        sysCtrlCfgQueryCondition.setCtrlCode(InsuranceConstant.CODE_INS_CTRL_CD_3);
        String last=sysCtrlCfgApiService.searchSysCtrlCfgByCtrlCode(sysCtrlCfgQueryCondition);
        Date now =new Date();
        int days=0;
        if(StringUtils.isNotEmpty(last)){
            days=Integer.parseInt(last);
            condition.setPlcEndTm(DateUtils.getDateAddDate(now,5,days));
        }
        cn.fl.preloan.condition.InsPrjInfoQueryCondition insPrjInfoQueryCondition = new cn.fl.preloan.condition.InsPrjInfoQueryCondition();
        BeanUtils.copyProperties(condition,insPrjInfoQueryCondition);
        long start = System.currentTimeMillis();
        BaseLogUtils.info(logger,"initList","调用贷前服务-续保分页查询开始");
        ResponseData<ListInsurancePolicyResponseDTO> insurancePage = iInsPlcBscInfoService.listInsurancePage(page.getCurrentPage(),page.getEveryPage(),insPrjInfoQueryCondition,CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        BaseLogUtils.info(logger,"initList","调用贷前服务-续保信息分页查询结束，用时：" + (System.currentTimeMillis() - start));
        if(null != insurancePage && CollectionUtils.isNotEmpty(insurancePage.getData().getInsPrjInfoDTOS())){
            List<cn.fl.preloan.dto.InsPrjInfoDTO> insPrjInfoDTOS=insurancePage.getData().getInsPrjInfoDTOS();
            List<InsPrjInfoVO> voList = new ArrayList<>();
            for (cn.fl.preloan.dto.InsPrjInfoDTO insPrjInfoDTO:insPrjInfoDTOS){
                InsPrjInfoVO insPrjInfoVO = new InsPrjInfoVO();
                BeanUtils.copyProperties(insPrjInfoDTO,insPrjInfoVO);
                voList.add(insPrjInfoVO);
            }
            String tempOrg="";
            for(InsPrjInfoVO vo:voList){
                //所属省公司
                if(StringUtils.isNotBlank(vo.getCstMgrOrgCd())){
                    if(vo.getCstMgrOrgCd().length()>=12){
                        tempOrg=vo.getCstMgrOrgCd().substring(0,12);
                    }else{
                        tempOrg=vo.getCstMgrOrgCd();
                    }
                    SysOrgDTO orgDTO=sysOrgApiService.selectSysOrgByOrgCd(tempOrg,InsuranceConstant.CODE_INS_SYS_CD);
                    if(null!=orgDTO){
                        vo.setCstMgrOrgCdNm(orgDTO.getOrgDispNm());
                    }
                }
            }
            page.setTotalCount(insurancePage.getData().getTotal());
            page.setResultList(voList);
        }
        return page;
    }

    /**
     * 分页查询 保险管理
     *
     * @param page
     * @return
     */
    @Override
    public Pager searchPlcMgrListPage(Pager page) throws BaseException {
        if (null == page) page = new Pager();
        return this.insPlcBscInfoApiService.searchPlcMgrListPage(page, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<InsPlcBscInfoVO> searchList(InsPlcBscInfoQueryCondition condition) throws BaseException {
        if(null!=condition){
            condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
            condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        }
        List<InsPlcBscInfoVO> voList= InsPlcBscInfoVOTransform.toVOList(this.insPlcBscInfoApiService.searchInsPlcBscInfoList(condition));
        return voList;
    }

    /**
     * 查询险种数据字典
     * @return
     */
    public List<KeyValueVO> searchInsTypCdList(){
        /*InsTypInfoQueryCondition condition=new InsTypInfoQueryCondition();
        condition.setTypLvl(InsuranceConstant.CODE_INS_TYP_LVL_CD);//保险父类
        List<InsTypInfoDTO> dtoList=this.insTypInfoApiService.searchInsTypInfoList(condition);
        List<KeyValueVO> list=new ArrayList<>();
        KeyValueVO kv=null;
        for(InsTypInfoDTO dto:dtoList){
            kv=new KeyValueVO();
            kv.setKey(dto.getId().toString());
            kv.setValue(dto.getTypNm());
            list.add(kv);
        }
        return list;*/
        return this.sysDictionaryApiService.getKvList(InsuranceConstant.CODE_INS_TYP_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 查询购买方式数据字典
     * @return
     */
    public List<KeyValueVO> searchBuyWayCdList(){
        return this.sysDictionaryApiService.getKvList(InsuranceConstant.CODE_INS_BUY_WAY_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 查询保单期限数据字典
     * @return
     */
    public List<KeyValueVO> searchPlcExpCdList(){
        return this.sysDictionaryApiService.getKvList(InsuranceConstant.CODE_INS_PLC_EXP_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 查询保险性质数据字典
     * @return
     */
    public List<KeyValueVO> searchPlcPropCdList(){
        return this.sysDictionaryApiService.getKvList(InsuranceConstant.CODE_INS_PLC_NAT_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 查询保险产品类型数据字典
     * @return
     */
    public List<KeyValueVO> searchPrdTypCdList(){
        return this.sysDictionaryApiService.getKvList(InsuranceConstant.CODE_INS_PRD_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 查询保险特殊政策数据字典
     * @return
     */
    public List<KeyValueVO> searchPlcSpePolCdList(){
        return this.sysDictionaryApiService.getKvList(InsuranceConstant.CODE_INS_PLC_SPE_POL_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 查询续保状态数据字典
     */
    @Override
    public List<KeyValueVO> searchReneStsCdList() {
        return this.sysDictionaryApiService.getKvList(InsuranceConstant.CODE_INS_PLC_RENE_STS_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 根据组代码查询数据字典
     * @param grpCd
     * @return
     * @throws BaseException
     */
    public List<KeyValueVO> searchDictByGrpCd(String grpCd) throws BaseException{
        return this.sysDictionaryApiService.getKvList(grpCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 验证保单号是否重复
     *
     * @param plcNo
     * @return
     * @throws BaseException
     */
    @Override
    public boolean validatePlcNo(String plcNo) throws BaseException {
        InsPlcBscInfoQueryCondition condition=new InsPlcBscInfoQueryCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        //condition.setPlcNo(plcNo);
        condition.setPlcNoUnique(plcNo);
        List<InsPlcBscInfoDTO> list=this.insPlcBscInfoApiService.searchInsPlcBscInfoList(condition);
        if(CollectionUtils.isNotEmpty(list)){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 验证保险公司是否设置渠道政策
     * @param insCompId
     * @param prdTypCd
     * @param insTypCd
     * @return 未设置渠道政策返回true,已设置返回渠道政策ID
     * @throws BaseException
     */
    public Map<String,String> validateInsComp(Long insCompId,String prdTypCd,String insTypCd) throws BaseException{
        InsCompChnPolQueryCondition condition=new InsCompChnPolQueryCondition();
        condition.setInsCompId(insCompId);
        condition.setPrdTypCd(prdTypCd);
        condition.setInsTypCd(insTypCd);
        List<InsCompChnPolDTO> list=this.insCompChnPolApiService.searchInsCompChnPolList(condition);
        Map<String,String> result=new HashMap<>(2);
        if(CollectionUtils.isNotEmpty(list)){
            result.put("chnPol",String.valueOf(list.get(0).getId()));
            result.put("available","false");
        }else{
            result.put("available","true");//未设置渠道政策
        }
        return result;
    }

    /**
     * 验证项目产品对应的保险险种是否已录入
     *
     * @param prjPrdDtlId
     * @param insTypCd
     * @return
     * @throws BaseException
     */
    @Override
    public boolean validateInsTyp(Long prjPrdDtlId, String insTypCd) throws BaseException {
        InsPlcBscInfoQueryCondition condition = new InsPlcBscInfoQueryCondition();
        condition.setPrjPrdDtlId(prjPrdDtlId);
        condition.setInsTypCd(insTypCd);
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());

        List list = this.insPlcBscInfoApiService.searchInsPlcBscInfoList(condition);
        if(CollectionUtils.isNotEmpty(list)&&list.size()>0){
            return false;
        }
        return true;
    }

    /**
     * 查询保险客户信息
     * @param cstId
     * @return
     * @throws BaseException
     */
    public CstBscInfoDTO selectPlcCstInfo(Long cstId) throws BaseException{
        return this.customerQueryApiService.getCstBscInfoById(cstId);
    }

    /**
     * 查询续保产品保单信息
     * @param condition
     * @return
     * @throws BaseException
     */
    @Override
    public List<InsPrjInfoVO> searchRenewalPrdList(InsPrjInfoQueryCondition condition) throws BaseException{
        if(null==condition){
            condition=new InsPrjInfoQueryCondition();
        }
        SysCtrlCfgQueryCondition sysCtrlCfgQueryCondition=new SysCtrlCfgQueryCondition();
        sysCtrlCfgQueryCondition.setOrgCd(InsuranceConstant.CODE_INS_ORG_CD);
        sysCtrlCfgQueryCondition.setSysCd(InsuranceConstant.CODE_INS_SYS_CD);
        sysCtrlCfgQueryCondition.setCtrlCode(InsuranceConstant.CODE_INS_CTRL_CD_3);
        String last=sysCtrlCfgApiService.searchSysCtrlCfgByCtrlCode(sysCtrlCfgQueryCondition);
        Date now =new Date();
        int days=0;
        if(StringUtils.isNotEmpty(last)){
            days=Integer.parseInt(last);
            condition.setPlcEndTm(DateUtils.getDateAddDate(now,5,days));
        }

        List<InsPrjInfoDTO> list= this.insPlcBscInfoApiService.searchRenewalPrdList(condition);
        if(CollectionUtils.isNotEmpty(list)){
            List<InsPrjInfoVO> voList=InsPrjInfoVOTransform.toVOList(list);
            List<Long> cstIdList=new ArrayList<>(1);
            cstIdList.add(voList.get(0).getCstId());

            //查询联系人电话
            CstContactInfoDTO contactInfoDTO=null;
            List<CstContactInfoDTO> contactInfoDTOList=customerQueryApiService.searchCstContactInfo(cstIdList);
            if(CollectionUtils.isNotEmpty(contactInfoDTOList)){
                BaseLogUtils.info(logger,"searchRenewalPrdList","查询续保产品保单信息，查询客户联系人==>"+contactInfoDTOList.size());
                contactInfoDTO=contactInfoDTOList.get(0);
            }else{
                BaseLogUtils.info(logger,"searchOvdPayDtlList","查询续保产品保单信息，查询客户联系人==>返回空");
            }
            //续保状态
            Map<String,String> reneStsCdMap=this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),InsuranceConstant.CODE_INS_PLC_RENE_STS_CD);
            //资产状态
            Map<String,String> prdStsCdMap=this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(), PrjPrdDtlConstant.CODE_PRJ_PRD_STS_CD);
            //保险种类
            Map<String,String> insTypCdMap=this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),InsuranceConstant.CODE_INS_TYP_CD);



            for(InsPrjInfoVO vo:voList){
                if(null!=contactInfoDTO){
                    vo.setCstNm(vo.getCstNm());
                    vo.setLkPhone(contactInfoDTO.getMp1());
                }
                if(CollectionUtils.isMapNotEmpty(reneStsCdMap)&&reneStsCdMap.containsKey(vo.getReneStsCd())){
                    vo.setReneStsCdNm(reneStsCdMap.get(vo.getReneStsCd()));
                }
                if(CollectionUtils.isMapNotEmpty(prdStsCdMap)&&prdStsCdMap.containsKey(vo.getPrdStsCd())){
                    vo.setPrdStsCdNm(prdStsCdMap.get(vo.getPrdStsCd()));
                }
                if(CollectionUtils.isMapNotEmpty(insTypCdMap)&&insTypCdMap.containsKey(vo.getInsTypCd())){
                    vo.setInsTypCdNm(insTypCdMap.get(vo.getInsTypCd()));
                }

            }

            return voList;
        }
        return null;
    }

    /**
     * 查询项目信息
     * @param prjId
     * @return
     * @throws BaseException
     */
    @Override
    public PrjBscInfoVO searchPrjBscInfo(Long prjId) throws BaseException{
         return PrjBscInfoVOTransform.toVO(this.projectApiService.selectById(prjId));
    }

    /**
     * 发起续保申请
     *
     * @param condition
     * @return
     * @throws BaseException
     */
    @Override
    public DataResultVO addRenewApply(InsPlcBscInfoQueryCondition condition) throws BaseException {
        DataResultVO resultVO= null;
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        condition.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());
        condition.setUsrNm(CurrentThreadContext.getCurrentUserName());
        //发起续保流程
        try {
            DataResultDTO resultDTO = this.insPlcBscInfoApiService.addRenewApply(condition);
            if (resultDTO != null) {
                resultVO = new DataResultVO(Integer.parseInt(resultDTO.getCode()), resultDTO.getInfo());
            } else {
                resultVO = new DataResultVO(BaseConstant.IS_YESNO_NO, "流程发起失败");
            }
        } catch (Exception e) {
            BaseLogUtils.info(logger, "addRenewApply", "发起续保申请=" + JSON.toJSONString(condition), e);
            resultVO = new DataResultVO(BaseConstant.IS_YESNO_NO, StringUtils.isBlank(e.getMessage()) ? "流程发起失败" : e.getMessage());
        }
        BaseLogUtils.info(logger, "addRenewApply", "发起续保申请=" + JSON.toJSONString(condition) + "；返回=" + JSON.toJSONString(resultVO));
        return resultVO;
    }

    /**
     * 修改保单保证金处理方式
     *
     * @param condition
     * @return
     * @throws BaseException
     */
    @Override
    public DataResultVO modifyHandleWay(InsPlcBscInfoQueryCondition condition) throws BaseException {
        DataResultVO resultVO= null;
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        condition.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());
        condition.setUsrNm(CurrentThreadContext.getCurrentUserName());
        DataResultDTO resultDTO=this.insPlcBscInfoApiService.modifyHandleWay(condition);
        if(resultDTO!=null){
            resultVO=new DataResultVO(Integer.parseInt(resultDTO.getCode()),resultDTO.getInfo());
        }else{
            resultVO=new DataResultVO(BaseConstant.IS_YESNO_NO,"修改保单保证金处理方式失败");
        }
        return resultVO;
    }

    /**
     * 保单附件配置列表
     *
     * @param condition
     * @return
     * @throws BaseException
     */
    @Override
    public List<PlcFileInfoVO> searchPlcCfgFileInfoList(InsPlcBscInfoQueryCondition condition) throws BaseException {
        if(StringUtils.isNotEmpty(condition.getCfgCd())){
            String[] cfgCdArray=condition.getCfgCd().split(",");
            List<String> cfgCdList = new ArrayList<>(cfgCdArray.length);
            for(String cfgCd:cfgCdArray){
                cfgCdList.add(cfgCd);
            }
            return getUploadFileList(StringUtils.isEmpty(condition.getId())?"":""+condition.getId(),cfgCdList,condition.getBuOrgCd());
        }
        return null;
    }

    /**
     * 删除保单上传的文件
     *
     * @param id
     * @return
     * @throws BaseException
     */
    @Override
    public boolean removePlcFile(Long id) throws BaseException {
        List<Long> list = new ArrayList<>(1);
        list.add(id);
        return this.uploadApiService.removeSysUlFilesRecByIds(list);
    }

    /**
     *
     * @param dataId 业务主键
     * @param cfgCdList 配置编号集合
     * @return
     * @throws BaseException
     */
    private List<PlcFileInfoVO> getUploadFileList(String dataId, List<String> cfgCdList, String orgCd) throws BaseException {
        //查询资料配置，系统代码,组织架构代码,配置编号 ,业务主键
        SysUlDatCfgQueryCondition ulDatCfgQueryCondition = new SysUlDatCfgQueryCondition();
        ulDatCfgQueryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());//系统代码
        ulDatCfgQueryCondition.setOrgCd(orgCd);//组织架构代码
        ulDatCfgQueryCondition.setDataId(dataId);//业务主键
        ulDatCfgQueryCondition.setCfgCdList(cfgCdList);//配置编号集合
        List<SysUlDatCfgDTO> list = null;
        if(StringUtils.isEmpty(dataId)){
            list = uploadApiService.searchSysUlDatCfgList(ulDatCfgQueryCondition);
        }else{
            list = uploadApiService.searchSysUlDatCfgListByDataId(ulDatCfgQueryCondition);
        }
        List<PlcFileInfoVO> voList = null;
        if (CollectionUtils.isNotEmpty(list)) {
            Map<String, String> audStsCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapitalConstant.CODE_MATE_AUD_STS_CD);
            voList = new ArrayList<>();
            PlcFileInfoVO vo;
            for (SysUlDatCfgDTO dto : list) {
                for (SysUlDatCfgDtlDTO dtlDTO : dto.getSysUlDatCfgDtlDTOList()) {
                    vo = new PlcFileInfoVO();
                    if(StringUtils.isNotEmpty(dataId)){
                        vo.setId(Long.valueOf(dataId));//业务主键
                    }
                    vo.setCfgDtlCd(dtlDTO.getCfgDtlCd());//配置子编号
                    vo.setDatNm(dtlDTO.getDatNm());//配置名称
                    vo.setCfgCd(dtlDTO.getCfgCd());//配置编号
                    vo.setCfgNm(dto.getCfgNm());//配置子名称
                    vo.setFileId(dtlDTO.getRecId());//附件ID
                    vo.setFilePath(dtlDTO.getFilePath());//附件路径
                    vo.setFileNm(dtlDTO.getRecFileNm());//上传名称
                    vo.setCrtTm(dtlDTO.getRecCrtTm());//上传时间
                    if (StringUtils.isNotBlank(dtlDTO.getRecAudStsCd())) {
                        vo.setAudStsCd(dtlDTO.getRecAudStsCd());//验证状态
                    } else {
                        vo.setAudStsCd("0");//验证状态
                    }

                    if (audStsCdMap != null && audStsCdMap.containsKey(vo.getAudStsCd())) {
                        vo.setAudStsCdNm(audStsCdMap.get(vo.getAudStsCd()));
                    }
                    vo.setOrgCd(dtlDTO.getOrgCd());
                    vo.setSysCd(dtlDTO.getSysCd());
                    voList.add(vo);
                }
            }
        }
        return voList;
    }

    /**
     * 查询保险第一受益人数据字典
     * @return
     */
    @Override
    public List<KeyValueVO> searchInsFstPerTypCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(InsuranceConstant.CODE_INS_PLC_FST_PER_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }
    /**
     * 查询保单信息
     * @return
     */
	@Override
	public List<InsPlcBscInfoVO> selectListLicNo() throws BaseException {
		// TODO Auto-generated method stub
		return InsPlcBscInfoVOTransform.toVOList(this.insPlcBscInfoApiService.selectListPlcNo());
	}


}
